
import b2Vec2 = Box2D.Common.Math.b2Vec2;
import b2BodyDef = Box2D.Dynamics.b2BodyDef;
import b2Body = Box2D.Dynamics.b2Body;
import b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape;
import b2FixtureDef = Box2D.Dynamics.b2FixtureDef;
import b2CircleShape = Box2D.Collision.Shapes.b2CircleShape;


class TestBox2d {

    private world: Box2D.Dynamics.b2World;
    private debug: Box2D.Dynamics.b2DebugDraw;
    private p2m: number = 30;
    private step: number = 30;


    public loop(e: egret.Event) {
        this.world.Step(1 / this.step, 3, 6);
        if (this.debug != null) {
            this.world.DrawDebugData();
        }
    }

    public createWorld(scene: egret.DisplayObjectContainer): void {
        var gravity: b2Vec2 = new b2Vec2(0, 10 * this.step / this.p2m);
        this.world = new Box2D.Dynamics.b2World(gravity, true);
        scene.addEventListener(egret.Event.ENTER_FRAME, this.loop, this);
    }

    public createDebug(scene: egret.DisplayObjectContainer): void {
        this.debug = new Box2D.Dynamics.b2DebugDraw();

        var s: egret.Sprite = new egret.Sprite();
        scene.addChild(s);
        this.debug.SetSprite(s);
        //this.debug.SetSprite(document.getElementsByTagName("canvas")[0].getContext("2d"));

        this.debug.SetDrawScale(this.p2m);
        this.debug.SetLineThickness(1);
        this.debug.SetAlpha(0.8);
        this.debug.SetFillAlpha(0.5);
        this.debug.SetFlags(Box2D.Dynamics.b2DebugDraw.e_shapeBit);
        this.world.SetDebugDraw(this.debug);
    }

    public createBox(posX: number, posY: number, w: number, h: number, isStatic: boolean = false): b2Body {
        posX = posX / this.p2m;
        posY = posY / this.p2m;
        w = w / this.p2m / 2;
        h = h / this.p2m / 2;
        var bodyDef: b2BodyDef = new b2BodyDef();
        bodyDef.position = new b2Vec2(posX, posY);
        if (isStatic) {
            bodyDef.type = b2Body.b2_staticBody;
        } else {
            bodyDef.type = b2Body.b2_dynamicBody;
        }

        var body: b2Body = this.world.CreateBody(bodyDef);
        var poly: b2PolygonShape = b2PolygonShape.AsBox(w, h);

        var fixtureDef = new b2FixtureDef();
        fixtureDef.density = 1;
        fixtureDef.restitution = 0;
        fixtureDef.shape = poly;

        body.CreateFixture(fixtureDef);
        return body
    }

    public createCircle(posX: number, posY: number, radius: number, isStatic: boolean = false, bodyDef: b2BodyDef = null, fixtureDef: b2FixtureDef = null): b2Body {
        posX = (posX) / this.p2m;
        posY = (posY) / this.p2m;
        radius = radius / this.p2m;
        if (bodyDef == null) {
            bodyDef = new b2BodyDef();
        }
        bodyDef.position = new b2Vec2(posX, posY);
        if (isStatic) {
            bodyDef.type = b2Body.b2_staticBody;
        } else {
            bodyDef.type = b2Body.b2_dynamicBody;
        }
        var body: b2Body = this.world.CreateBody(bodyDef);

        var poly: b2CircleShape = new b2CircleShape()
        //poly.SetLocalPosition(new b2Vec2(posX, posY))
        poly.SetRadius(radius)

        if (fixtureDef == null) {
            fixtureDef = new b2FixtureDef();
        }
        fixtureDef.density = 1;
        fixtureDef.restitution = 0;
        fixtureDef.shape = poly;

        body.CreateFixture(fixtureDef);
        return body
    }
}